package com.example.demo.ojcode;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-12-03
 * Time:14:03
 */
public class FileOperator {
    //读文件
    public static String readFile(String file) {
        StringBuilder s = new StringBuilder();
        try(FileReader fileReader = new FileReader(file)) {
            while (true) {
                int ch =  fileReader.read();
                if(ch == -1) {
                    break;
                } else {
                    s.append(ch);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s.toString();
    }
    //写文件
    public static void writeFile(String file,String content) {
        try(FileWriter fileWriter = new FileWriter(file);) {
            fileWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
